package main

//请求body参数bind到结构体
import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)
/**
 将入参直接返回给结构体
http://localhost:8080/bind?name=ttt&address=12&birthday=2019-09-01

 */

type Person struct {
	Name     string    `form:"name"`
	Address  string    `form:"address"`
	Birthday time.Time `form:"birthday" time_format:"2006-01-01"`
}

func main() {
	r := gin.Default()

	r.GET("/bind", paramsStructBind)
	r.POST("/bind", paramsStructBind)
	r.Run()
}

func paramsStructBind(context *gin.Context) {
	var person Person

	if err := context.ShouldBind(&person); err == nil {
		context.String(http.StatusOK, "%v", person)
	} else {
		context.String(http.StatusBadRequest, "bind err %v", err)
	}
}
